﻿#include "fswidget.h"
#include "ui_fswidget.h"

#include <QMessageBox>
#include <QDialog>
#include <QDebug>
#include <QKeyEvent>
#include <QtSql>
#include <QFileDialog>
#include <QPrinter>
#include <QTextDocument>
#include <QTextBlock>

int FSwidget::identification=0;
FSwidget::FSwidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FSwidget)
{
    ui->setupUi(this);
    FileSend();
}

FSwidget::~FSwidget()
{
    delete ui;
}

void FSwidget::FileSend()
{
    ui->tbWidgetPage->setSelectionBehavior(QAbstractItemView::SelectRows);  //整行选中的方式
    ui->tbWidgetPage->setEditTriggers(QAbstractItemView::NoEditTriggers);//不可修改
    ui->tbWidgetPage->setSelectionMode(QAbstractItemView::SingleSelection);  //设置为可以选中单个
    ui->tbWidgetRow->setSelectionBehavior(QAbstractItemView::SelectRows);  //整行选中的方式
    ui->tbWidgetRow->setEditTriggers(QAbstractItemView::NoEditTriggers);//不可修改
    ui->tbWidgetRow->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tbWidgetPage->horizontalHeader()->setResizeMode(QHeaderView::Stretch); //自适应列宽
    ui->tbWidgetRow->horizontalHeader()->setResizeMode(QHeaderView::Stretch); //自适应列宽
    //设置日历控件当前时间
    ui->dtEdtSmEnd->setDate(QDate::currentDate());
    ui->dtEdtSmStart->setDate(QDate::currentDate());

    ui->tbWidgetPage->setColumnHidden(0, true);//隐藏首列id
    ui->tbWidgetRow->setColumnHidden(0, true);//隐藏首列id
    ui->tbWidgetRow->setColumnHidden(1, true);//隐藏第二列id
    connect(ui->tbWidgetPage->horizontalHeader(),SIGNAL(sectionClicked(int)),this,SLOT(FSshowsort(int)));
    QSqlQuery query;
    QString strSelect=QString("SELECT * FROM FileSbmtPage limit 20;");
    query.exec(strSelect);
    while (query.next()) {
        ui->tbWidgetPage->insertRow(0);
        for(int a=0 ; a<7 ; a++){
            ui->tbWidgetPage->setItem(0,a,new QTableWidgetItem(query.value(a).toString()));
        }
    }
}


void FSwidget::on_chkBxSmStart_clicked(bool checked)
{
    ui->dtEdtSmStart->setEnabled(checked);
}

void FSwidget::on_chkBxSmEnd_clicked(bool checked)
{
    ui->dtEdtSmEnd->setEnabled(checked);
}

void FSwidget::showpage()
{
    ui->txtQuestion->clear();
    ui->txtSuggestion->clear();
    ui->tbWidgetPage->clearContents();
    ui->tbWidgetPage->setRowCount(0);
    ui->tbWidgetRow->clearContents();
    ui->tbWidgetRow->setRowCount(0);
    QSqlQuery query;
    QString strSelect=QString("SELECT * FROM FileSbmtPage;");
    query.exec(strSelect);
    while (query.next()) {
        ui->tbWidgetPage->insertRow(0);
        for(int a=0 ; a<7 ; a++){
            ui->tbWidgetPage->setItem(0,a,new QTableWidgetItem(query.value(a).toString()));
        }
    }
}

void FSwidget::showrow(QString x)
{
    ui->tbWidgetRow->clearContents();
    ui->tbWidgetRow->setRowCount(0);
    QSqlQuery query;
    QString strSelect=QString("SELECT * FROM FileSbmtRow WHERE FileSbmtID = '%1';").arg(x);
    query.exec(strSelect);
    while (query.next()) {
        ui->tbWidgetRow->insertRow(0);
        for(int a=0 ; a<8 ; a++){
            if(a == 3){
                switch (query.value(a).toInt()) {
                case 1:
                    ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem("居民身份证"));
                    break;
                case 2:
                    ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem("军官证"));
                    break;
                case 3:
                    ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem("士官证"));
                    break;
                case 4:
                    ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem("义务兵证"));
                    break;
                case 5:
                    ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem("港澳台居民居住证"));
                    break;
                case 6:
                    ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem("外籍人士在我国永久居住证"));
                    break;
                case 7:
                    ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem("护照"));
                    break;
                case 9:
                    ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem("其他"));
                    break;
                default:
                    break;
                }
            }else{
                ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem(query.value(a).toString()));
            }
        }
    }
}

void FSwidget::on_FRadd_clicked()
{
    int countr=ui->tbWidgetPage->rowCount();
    FSaddDialog fsadd(this);
    fsadd.exec();
    if(ui->tbWidgetPage->rowCount()==(countr+1))
    {ui->tbWidgetPage->selectRow(0);
    on_tbWidgetPage_cellClicked(0,0);}
}

void FSwidget::on_tbWidgetPage_cellClicked(int row, int column)
{
    if(row>-1&&column>-1){
        ui->tbWidgetRow->clearContents();
        ui->tbWidgetRow->setRowCount(0);
        ui->txtQuestion->clear();
        ui->txtSuggestion->clear();
        QString pageID = ui->tbWidgetPage->item(row,0)->text();\
        QSqlQuery query;
        QString strSelect=QString("SELECT * FROM FileSbmtRow WHERE FileSbmtID = '%1';").arg(pageID);
        query.exec(strSelect);
        while (query.next()) {
            ui->tbWidgetRow->insertRow(0);
            for(int a=0 ; a<8 ; a++){
                if(a == 3){
                    switch (query.value(a).toInt()) {
                    case 1:
                        ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem("居民身份证"));
                        break;
                    case 2:
                        ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem("军官证"));
                        break;
                    case 3:
                        ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem("士官证"));
                        break;
                    case 4:
                        ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem("义务兵证"));
                        break;
                    case 5:
                        ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem("港澳台居民居住证"));
                        break;
                    case 6:
                        ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem("外籍人士在我国永久居住证"));
                        break;
                    case 7:
                        ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem("护照"));
                        break;
                    case 9:
                        ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem("其他"));
                        break;
                    default:
                        break;
                    }
                }else{
                    ui->tbWidgetRow->setItem(0,a,new QTableWidgetItem(query.value(a).toString()));
                }
            }
        }
        QSqlQuery query2;
        QString strSelect2=QString("SELECT FileLvQstn,FileSignSgstn FROM FileSbmtPage WHERE FileSbmtID = '%1';").arg(pageID);
        query2.exec(strSelect2);
        while(query2.next()){
            QString strQuestion = query2.value(0).toString();
            QString strSuggestion = query2.value(1).toString();
            ui->txtQuestion->setText(strQuestion);
            ui->txtSuggestion->setText(strSuggestion);
        }
    }
}

void FSwidget::on_FRdelete_clicked()
{
    if(ui->tbWidgetPage->currentRow()>-1){
    QSqlQuery query;
    int ID=ui->tbWidgetPage->item(ui->tbWidgetPage->currentRow(),0)->text().toInt();
    QString strsql = QString("DELETE FROM FileSbmtPage WHERE FileSbmtID='%1';").arg(ID);
    if(query.exec(strsql)) {QMessageBox::information(this,"提示信息","删除成功!");
    on_FRseek_clicked();}
    else {QMessageBox::information(this,"提示信息","删除失败!");}
    }else{QMessageBox::information(this, tr("提示信息"), tr("无法删除：未选中数据！"));}
}

void FSwidget::on_FRseek_clicked()
{
    int select;
    if(ui->tbWidgetPage->currentRow()>-1)
    {
        select=ui->tbWidgetPage->item(ui->tbWidgetPage->currentRow(),0)->text().toInt();
    }
    ui->txtQuestion->clear();
    ui->txtSuggestion->clear();
    QString strFromUnit = ui->edtFromUnit->text().trimmed();
    QString strName = ui->edtName->text().trimmed();
    QString strSbmtTimeStart = ui->dtEdtSmStart->date().toString("yyyy-MM-dd");
    QString strSbmtTimeEnd = ui->dtEdtSmEnd->date().toString("yyyy-MM-dd");
    ui->tbWidgetPage->clearContents();
    ui->tbWidgetPage->setRowCount(0);
    ui->tbWidgetRow->clearContents();
    ui->tbWidgetRow->setRowCount(0);
    QString strSql = QString("SELECT distinct a.FileSbmtID,a.FileSbmTime,a.FileSbmtUnit,a.FileSignName,a.FileSignTime,a.FileDrgName,a.FileSbmTel FROM FileSbmtPage a,FileSbmtRow b WHERE a.FileSbmtID=b.FileSbmtID ");
    if(!strFromUnit.isEmpty()){
        strSql += QString("AND a.FileSbmtUnit = '%1'").arg(strFromUnit);
    }
    if(ui->chkBxSmStart->isChecked()){
        strSql += QString("AND a.FileSbmTime >= '%1'").arg(strSbmtTimeStart);
    }
    if(ui->chkBxSmEnd->isChecked()){
        strSql += QString("AND a.FileSbmTime <= '%1'").arg(strSbmtTimeEnd);
    }
    if(!strName.isEmpty()){
        strSql += QString(" AND b.FilePerName= '%1'").arg(strName);
    }
    QSqlQuery query;
    query.exec(strSql);
    while (query.next()) {
        ui->tbWidgetPage->insertRow(0);
        for(int a=0 ; a<7 ; a++){
            ui->tbWidgetPage->setItem(0,a,new QTableWidgetItem(query.value(a).toString()));
        }
    }
    if(select>0){
        for(int j=0;j<ui->tbWidgetPage->rowCount();j++)
            {
           if(ui->tbWidgetPage->item(j,0)->text().toInt()==select){
               ui->tbWidgetPage->selectRow(j);
               on_tbWidgetPage_cellClicked(j,0);
           }}}
}

void FSwidget::on_FRedit_clicked()
{
    int i=ui->tbWidgetPage->currentRow();
    if(i>-1){
        FSEditDialog fsedit(this);
        connect(this,SIGNAL(sendD(QString)),&fsedit,SLOT(receiveID(QString)));
        emit sendD(ui->tbWidgetPage->item(i,0)->text());
        fsedit.exec();
        on_FRseek_clicked();
    }
    else{
    QMessageBox::information(this,"提示信息","未选中编辑行!");
    }
}

void FSwidget::on_addrow_clicked()
{
    int i=ui->tbWidgetPage->currentRow();
    if(i>-1){
        if(ui->tbWidgetRow->rowCount()>2){
        QMessageBox::information(this,"提示信息","每个档案报送信息最多报送三人档案信息!");}
        else{FSaddrelateDialog fsaddr(this);
        connect(this,SIGNAL(sendD(QString)),&fsaddr,SLOT(receiveID(QString)));
        emit sendD(ui->tbWidgetPage->item(i,0)->text());
        fsaddr.exec();}
    }else{QMessageBox::information(this,"提示信息","请先选中添加档案的信息行!");}
}

void FSwidget::on_deleterow_clicked()
{
    if(ui->tbWidgetPage->currentRow()>-1){
    if(ui->tbWidgetRow->currentRow()>-1)
    {
    QSqlQuery query;
    int ID=ui->tbWidgetRow->item(ui->tbWidgetRow->currentRow(),0)->text().toInt();
    QString strsql = QString("DELETE FROM FileSbmtRow WHERE FileStRowID='%1';").arg(ID);
    if(query.exec(strsql)) {QMessageBox::information(this,"提示信息","删除成功!");
    showrow(ui->tbWidgetPage->item(ui->tbWidgetPage->currentRow(),0)->text());}
    else {QMessageBox::information(this,"提示信息","删除失败!");}
    }else{QMessageBox::information(this, tr("提示信息"), tr("未选中删除行！"));}
    }else{QMessageBox::information(this, tr("提示信息"), tr("无法删除：未选中数据！"));}
}

void FSwidget::on_editrow_clicked()
{
    if(ui->tbWidgetPage->currentRow()>-1){
    int rowcou=ui->tbWidgetRow->currentRow();
    if(rowcou>-1)
    {FSeditrelateDialog fseditr(this);
        connect(this,SIGNAL(sendD(QString)),&fseditr,SLOT(receiveID(QString)));
        emit sendD(ui->tbWidgetRow->item(rowcou,0)->text());
        fseditr.exec();
        showrow(ui->tbWidgetPage->item(ui->tbWidgetPage->currentRow(),0)->text());
        ui->tbWidgetRow->selectRow(rowcou);
    }else{QMessageBox::information(this, tr("提示信息"), tr("未选中编辑行！"));}
    }else{QMessageBox::information(this, tr("提示信息"), tr("无法编辑：未选中数据！"));}
}

void FSwidget::on_FRPDF_clicked()
{
    int i=ui->tbWidgetPage->currentRow();
    if(i>-1){
        QString filePth = QCoreApplication::applicationDirPath() + "/";
        QString fileName = QFileDialog::getSaveFileName(this, tr("导出PDF文件"), filePth, "*.pdf");
        if (!fileName.isEmpty())
        {
            // 如果文件后缀为空，则默认使用.pdf
            if (QFileInfo(fileName).suffix().isEmpty())
            {
                fileName.append(".pdf");
            }
            QPrinter *printer = new QPrinter(QPrinter::ScreenResolution);
            // 指定输出格式为pdf
            printer->setPageSize(QPrinter::A4);
            printer->setOutputFormat(QPrinter::PdfFormat);
            printer->setOutputFileName(fileName);
            QTextDocument text_document;
            text_document.setHtml(saveHtmlToPDF(fileName));
            text_document.print(printer);
            text_document.end();
        }
    }else{
        QMessageBox::information(this,"提示信息","未选择打印条目行!");
    }
}


QString FSwidget::saveHtmlToPDF(QString Q)
{
    QString html = "";
    html += "<html>";
    html += "<head>";
    html += "<title>"+Q+"</title>";
    html += "<head>";
    html += "<body>";
    html += "<font size=\"4\" face=\"arial\">";
    html += "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"10\" align=\"center\">";
    html += "<tr>";
        html += "<th colspan=\"4\">档案报送单</th>";
    html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">报送单位：</td>";
            html += "<td align=\"center\">"+ui->tbWidgetPage->item(ui->tbWidgetPage->currentRow(),2)->text()+"</td>";
            html += "<td align=\"center\">报送时间：</td>";
            html += "<td align=\"center\">"+ui->tbWidgetPage->item(ui->tbWidgetPage->currentRow(),1)->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">签发人：</td>";
            html += "<td align=\"center\" >"+ui->tbWidgetPage->item(ui->tbWidgetPage->currentRow(),3)->text()+"</td>";
            html += "<td align=\"center\">签发日期：</td>";
            html += "<td align=\"center\" >"+ui->tbWidgetPage->item(ui->tbWidgetPage->currentRow(),4)->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">经办人：</td>";
            html += "<td align=\"center\">"+ui->tbWidgetPage->item(ui->tbWidgetPage->currentRow(),5)->text()+"</td>";
            html += "<td align=\"center\">联系电话：</td>";
            html += "<td align=\"center\">"+ui->tbWidgetPage->item(ui->tbWidgetPage->currentRow(),6)->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">档案遗留问题或需要说明的情况：</td>";
            html += "<td align=\"center\" colspan=\"3\">"+ui->txtQuestion->toPlainText()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">签发意见：</td>";
            html += "<td align=\"center\" colspan=\"3\">"+ui->txtSuggestion->toPlainText()+"</td>";
        html += "</tr>";
    html += "</table>";

    html += "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"10\" align=\"center\">";
    html += "<tr>";
        html += "<th colspan=\"6\">档案人信息</th>";
    html += "</tr>";
    html += "<tr>";
        html += "<td align=\"center\">姓名</td>";
        html += "<td align=\"center\">证件类型</td>";
        html += "<td align=\"center\">证件号</td>";
        html += "<td align=\"center\">职务</td>";
        html += "<td align=\"center\">任职时间</td>";
        html += "<td align=\"center\">卷数</td>";
    html += "</tr>";
    int rowCount = ui->tbWidgetRow->rowCount();
    for(int i=0 ; i<rowCount ; i++){
        html += "<tr>";
            html += "<td align=\"center\">"+ui->tbWidgetRow->item(i,2)->text()+"</td>";
            html += "<td align=\"center\">"+ui->tbWidgetRow->item(i,3)->text()+"</td>";
            html += "<td align=\"center\">"+ui->tbWidgetRow->item(i,4)->text()+"</td>";
            html += "<td align=\"center\">"+ui->tbWidgetRow->item(i,5)->text()+"</td>";
            html += "<td align=\"center\">"+ui->tbWidgetRow->item(i,6)->text()+"</td>";
            html += "<td align=\"center\">"+ui->tbWidgetRow->item(i,7)->text()+"</td>";
        html += "</tr>";
    }
    html += "</table>";
    html += "</font>";
    html += "</body>";
    html += "< / html>";
    return html;
}

void FSwidget::FSshowsort(int clounm)
{
    if(identification==0){
    ui->tbWidgetPage->sortItems(clounm,Qt::DescendingOrder);
    identification=1;}
    else{ui->tbWidgetPage->sortItems(clounm,Qt::AscendingOrder);
    identification=0;}
}
